package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @program: foodApp
 * @description: java classes connection mysql
 * @author: PanKer
 * @create: 2021-11-19 08:52
 */
public class dbUtil {
    private static String dbName = "com.mysql.cj.jdbc.Driver";
    private static String dbUrl = "jdbc:mysql://182.92.131.42:3306/foodapp?useUnicode=true&characterEncoding=utf8";
    private static String dbUser = "root";
    private static String dbPass = "Lipanke203201.";


    public static Connection getConn() throws ClassNotFoundException, SQLException {
        Class.forName(dbName);
        Connection conn = null;
        conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
        return conn;
    }

    public static void closeConn(Connection conn) throws SQLException {
        if (conn != null) conn.close();
    }

    public static void main(String[] args) {    // 测试数据库连接
        Connection conn = null;
        try {
            conn = dbUtil.getConn();
            System.out.println("数据库连接成功！！！");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("数据库连接建立失败111？？？");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接建立失败222？？？");
        } finally {
            try {
                dbUtil.closeConn(conn);
                System.out.println("数据库成功关闭了！！！");
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("关闭数据库失败 QAQ");
            }
        }
    }
}
